const utils = require('../../utils/utils.js');
// const api = require('../../utils/api.js');
const indexStore = require('../../service/wx_store.js');

const goods = [
  {
    src: '../../images/goods4.jpg',
  },
  {
    src: '../../images/goods5.jpg',
  },
  {
    src: '../../images/goods6.jpg',
  },
  {
    src: '../../images/goods7.jpg',
  },
  {
    src: '../../images/goods5.jpg',
  },
  {
    src: '../../images/goods6.jpg',
  },
  {
    src: '../../images/goods7.jpg',
  },
];

const submenuRight = [
  {
    src: '../../images/goods4.jpg',
  },
  {
    src: '../../images/goods5.jpg',
  },
  {
    src: '../../images/goods6.jpg',
  },
  {
    src: '../../images/goods7.jpg',
  },
  {
    src: '../../images/goods5.jpg',
  },
  {
    src: '../../images/goods6.jpg',
  },
  {
    src: '../../images/goods7.jpg',
  },
];

const topMenu = [{title: '评价'}, {title: '商家'}];

/**
 * submenu_left的元素是一个对象,有title和list两个属性
 * title {string} 子菜单标题
 */
const submenuLeft = [
  {title: '买过'},
  {title: '热销'},
  {title: '优惠'},
  {title: '烤小海鲜类'},
];

// pages/details/index.js
const wxStore = getApp().globalData.wx_store;
const page = Page;
page({
  onLoad: function(options) {
    const t = wxStore.getter();
    console.log('details:', t);
    this.generateGoods();
    this.generate_top_menu();
    this.generateSubmenu_left();
    this.generateSubmenu_right();
    utils.getElTop('.submenu-left').then((result) => {
      this.setData({submenuLeft: {top: result.top}});
    });
  },
  /**
   * @param {*} e
   * @return {*}
   */
  onPageScroll: function(e) {
    const onPageScrollPending = this.data.onPageScroll_pending;
    console.log(onPageScroll_pending);
    if (onPageScrollPending) {
      console.log('onPageScroll_pending');
      const setTimeoutCallBack = () => {
        this.setData({onPageScroll_pending: false});
      };
      setTimeout(setTimeoutCallBack, 3000);
      return 0;
    }
    const submenuLeft = this.data.submenuLeft;
    const top = submenuLeft.top;
    const isOk = e.scrollTop >= top;
    if (isOk) {
      this.setData({onPageScrollPending: true});
      wx.pageScrollTo({
        scrollTop: submenuLeft.top,
        duration: 0,
      });
    }
  },
  /**
   * 页面的初始数据
   */
  data: {
    goods,
    topMenu,
    submenuLeft,
    submenuRight,
    isLockDowo: false,
    onPageScrollPending: false,
    submenuLeft: {},
  },
  /**
   * 生成submenu_left所需的数据
   */
  generateSubmenu_left: function() {
    const length = submenuLeft.length;
    const _submenuLeft = [];
    for (let ii = 0; ii < 8; ii++) {
      for (let i = 0; i < length; i++) {
        const title = submenuLeft[i].title;
        const id = i;
        _submenuLeft.push({id, title});
      }
    }
    this.setData({submenuLeft: _submenuLeft});
  },
  /**
   * 生成submenu_right所需的数据
   */
  generateSubmenu_right: function() {
    const length = submenuRight.length;
    const _submenuRight = [];
    const _priceModuleData = [];
    const isOk = [true, false];
    for (let i = 0; i < length; i++) {
      const src = submenuRight[i].src;
      const title = '碳烤生蚝6只';
      const goodsAbstract = '满口糯糯的感觉,好吃到爆炸!';
      const isDiscount = isOk[Math.floor(Math.random() * isOk.length)];
      const price = (Math.random() * 100).toFixed(1);
      const rate = Math.random() * 0.8;
      const discountRate = (rate * 10).toFixed(1);
      const discountPrice = (price * rate).toFixed(1);
      const sellAmount = Math.floor(Math.random() * 1000);
      _submenuRight.push({
        src,
        title,
        goodsAbstract,
        isDiscount,
        price,
        discountRate,
        discountPrice,
        sellAmount,
      });
    }
    this.setData({
      submenu_right: _submenuRight,
      _priceModuleData: _priceModuleData,
    });
  },
  /**
   * 生成goods的数据
   */
  generateGoods: function() {
    const length = goods.length;
    const _goods = [];
    const isOk = [true, false];
    for (let i = 0; i < length; i++) {
      const src = goods[i].src;
      const isDiscount = isOk[Math.floor(Math.random() * isOk.length)];
      const price = (Math.random() * 100).toFixed(1);
      const discountPrice = (price * Math.random() * 0.8).toFixed(1);
      const sellAmount = Math.floor(Math.random() * 1000);
      _goods.push({src, isDiscount, price, discountPrice, sellAmount});
    }
    this.setData({goods: _goods});
  },
  /**
   * 生成top_menu的数据
   */
  generate_top_menu: function() {
    const length = topMenu.length;
    const _topMenu = [];
    for (let i = 0; i < length; i++) {
      _topMenu.push({
        title: topMenu[i].title,
        isDecorate: false,
        order: String(i + 1),
      });
    }
    this.setData({_topMenu: _topMenu});
  },

  /**
   * 改变所有top_menu数组元素中isDecorate属性的值
   * 点击元素isDecorate变为true,其余的变为false
   * @param {Event} e
   */
  tiggerDecorate: function(e) {
    const _index = e.currentTarget.dataset.index;
    const topMenu = this.data.top_menu;
    topMenu.forEach((val, index) => {
      if (index === _index) {
        val.isDecorate = true;
        val.order = topMenu.length + 1;
      } else {
        val.isDecorate = false;
        val.order = index;
      }
      this.setData({topMenu});
    });
  },
});
